package com.gao.test;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;

/**
 * @Author: ShanYuqing
 * @Version: v1.0
 */
public class Server {
    public static void main(String[] args) throws IOException {
        ServerSocket socket = new ServerSocket(8899);
        while(true){
            Socket accept = socket.accept();
            //创建网络字节输入流和输出流，用来接收和发送数据
            InputStream inputStream = accept.getInputStream();
            OutputStream outputStream = accept.getOutputStream();
            //包装成缓冲字符流，方便调用readLine方法读取一行字符串
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
            String fileName = reader.readLine();
            File file = new File("E://", UUID.randomUUID().toString()+fileName);
            writer.write("ok");
            writer.newLine();
            writer.flush();
            FileOutputStream outputStream1 = new FileOutputStream(file);
            int len;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes)) != -1) {
                outputStream1.write(bytes, 0, len);
            }
            outputStream1.close();
            writer.write("上传成功");
            writer.newLine();
            writer.flush();

            socket.close();
        }
        }
}
